<template>
  <div>
    <el-dialog title="提示" :visible.sync="showRole" width="30%" :before-close="dialogClose">
      <div>
        <p>当前用户：{{datas.username}}</p>
        <p>用户角色：{{datas.role_name}}</p>
      </div>分配新角色：
      <el-select v-model="value" placeholder="请选择">
        <el-option v-for="item in options" :key="item.id" :label="item.roleName" :value="item.id"></el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClose">取 消</el-button>
        <el-button type="primary" @click="dialogVisible">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [],
      value:''
    };
  },
  props: {
    showRole: {
      type: Boolean,
      defaule: true,
    },
    datas: {
      defaule: false,
    },
  },
  created() {
    this.$axios.get("roles").then((res) => {
      console.log(res);
      if (res.data.meta.status == 200) {
        this.options = res.data.data;
      } else {
        this.$message({
          message: "获取失败",
          type: error,
        });
      }
    });
  },
  methods: {
    dialogClose() {
        this.options='';
      this.$emit("closeRole");
    },
    //分配角色
    dialogVisible() {
          this.$axios.put(`users/${this.datas.id}/role`,{rid:this.value}).then(res=>{
            if(res.data.meta.status==200){
              this.$message({
                    message: res.data.meta.msg,
                type: "success",
              })
            }else{
                this.$message({
                    message: res.data.meta.msg,
                type: "error",
              })
            }
          })
      this.$emit("closeRole");
    },
  },
};
</script>
<style lang='scss' scoped>
</style>